package guowei.guliproduct.product.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import guowei.guliproduct.product.entity.CategoryEntity;
import guowei.guliproduct.product.service.CategoryService;
import guowei.common.utils.PageUtils;
import guowei.common.utils.R;



/**
 * 商品三级分类
 *
 * @author guoguo
 * @email 1573746397@qq.com
 * @date 2023-05-21 17:38:07
 */
@RestController
@Slf4j
@RequestMapping("/product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    /**
     * 得到商品的三级分类信息
     * @return
     */
    @GetMapping("/list/tree")
    public R getAllTrees(){
        List<CategoryEntity> lists = categoryService.getAlllists();
        return R.ok().put("lists",lists);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:category:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category1){
        log.info(category1.toString());
		categoryService.save(category1);

        return R.ok();
    }

    /**
     * 修改时，也同时修改与其有关联的字段 比如relation的catelogNames
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category){

        categoryService.updateDetails(category);

        return R.ok();
    }

    @RequestMapping("/update/list")
    public R updateList(@RequestBody CategoryEntity[] categoryEntities){
        categoryService.updateBatchById(Arrays.asList(categoryEntities));
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    //@RequiresPermissions("product:category:delete")
    public R delete(@RequestBody ArrayList<Long> catIds){

        categoryService.removeMenusByIds(catIds);

//		categoryService.removeByIds(Arrays.asList(catIds));

        return R.ok();
    }

    /**
     * 删除一个根据catId
     * @param
     * @return
     */
    @PostMapping("/delete2")
    public R delete2(@RequestBody CategoryEntity category1){
        log.info(category1.toString());
        categoryService.deleteOneByCatId(category1.getCatId());
        return R.ok();
    }

}
